<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信支付</title>

</head>
<body>

<div STYLE="font-size: 40px">
    <span class="up" onclick="wap_pay('upacp_wap')">支付</span>

    微信支付
</div>
    <script>
        var token = "7ddcf7b420de442db1bb33057f99a68a";
        var httpurl = "http://www.likewawa.com/miaozhuapc/v1/wx/wxpay?payConfigId=1&channel=JSAPI";
        var body2
        function wap_pay() {
        var YOUR_URL = httpurl;
        var xhr = new XMLHttpRequest();
        xhr.open("GET", YOUR_URL, true);
        //xhr.setRequestHeader("Content-type", "application/json");
        xhr.setRequestHeader('WAWA_TOKEN', token);
        xhr.send(JSON.stringify({
            "payConfigId":9,
            "channel": "JSAPI"
        }));
        var that = this;
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                console.log(xhr.responseText);

                body2 = JSON.parse(xhr.responseText).body;
                that.body2 = body2;
                console.dir(body2)
                onBridgeReady(body2.appId, body2.timeStamp, body2.nonceStr,  body2.package, body2.paySign);
            }
        }
        function onBridgeReady(appId, timeStamp,nonceStr,package2,paySign ){
            console.log("appId = "+ appId +"timeStamp = "+ timeStamp +"nonceStr = "+ nonceStr + "package = "+ package2 + "paySign = "+ paySign)
            alert("appId = "+ appId)
            alert("timeStamp = "+ timeStamp)
            alert("nonceStr = "+ nonceStr)
            alert("package = "+ package2)
            alert("paySign = "+ paySign)

            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                    "appId":"wx890410ff186d98b7",     //公众号名称，由商户传入
                    "timeStamp":timeStamp,         //时间戳，自1970年以来的秒数
                    "nonceStr":nonceStr, //随机串
                    "package":package2,
                    "signType":"MD5",         //微信签名方式：
                    "paySign":paySign //微信签名
                },
                function(res){
                    alert(res.toString())
                    alert(JSON.stringify(res))
                    console.dir(res)
                    if(res.err_msg == "get_brand_wcpay_request:ok" ) {

                    }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                }
            );
        }
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        }else{
            //onBridgeReady();
            //onBridgeReady(body2.appid, new Date().getTime(), body2.nonce_str,  "prepay_id="+body2.prepay_id, body2.sign);
        }
    }

    </script>
</body>
</html>